package com.zb.kc.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zb.kc.client.OrderClient;
import com.zb.kc.commonutils.JwtUtils;
import com.zb.kc.commonutils.R;
import com.zb.kc.commonutils.ordervo.CourseWebVoOrder;
import com.zb.kc.entity.EduCourse;
import com.zb.kc.entity.vo.ChapterVo;
import com.zb.kc.entity.vo.CourseQueryVo;
import com.zb.kc.entity.vo.CourseWebVo;
import com.zb.kc.service.IEduChapterService;
import com.zb.kc.service.IEduCourseService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/eduservice/coursefront")
public class CourseFrontController {

    @Autowired
    private IEduCourseService courseService;

    @ApiOperation(value = "分页课程列表")
    @PostMapping("{current}/{size}")
    public R pageList(@PathVariable("current") long current,
                      @PathVariable("size") long size,
                      @RequestBody(required = false) CourseQueryVo courseQueryVo) {
        Page<EduCourse> page = new Page<>(current, size);
        Page<EduCourse> eduCoursePage = courseService.pageListWeb(page, courseQueryVo);
        return R.ok().dataPage(eduCoursePage);
    }

    @Autowired
    private IEduChapterService chapterService;

    @Autowired
    private OrderClient orderClient;

    @ApiOperation(value = "根据ID查询课程详情")
    @GetMapping(value = "{courseId}")
    public R getById(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId, HttpServletRequest request) {
        //查询课程信息和讲师信息
        CourseWebVo courseWebVo = courseService.selectInfoWebById(courseId);
        //查询当前课程的章节和小节信息
        List<ChapterVo> chapterVoList = chapterService.getChapterVideoByCourseId(courseId);
        // 根据课程id和用户id查询课程的购买状态
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        boolean isBuy;
        if (StringUtils.isEmpty(memberId)) {
            isBuy = false;
            return R.ok().data("course", courseWebVo).data("chapterVoList", chapterVoList).data("isBuy", isBuy);
        }
        isBuy = orderClient.isBuyCourse(courseId, memberId);
        return R.ok().data("course", courseWebVo).data("chapterVoList", chapterVoList).data("isBuy", isBuy);
    }

    // 根据id查询课程信息
    @PostMapping("getCourseInfoOrder/{id}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable("id") String id) {
        CourseWebVo courseWebVo = courseService.selectInfoWebById(id);
        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(courseWebVo, courseWebVoOrder);
        return courseWebVoOrder;
    }


}
